#! /usr/bin/perl

use strict ;
use DBQueries ;
$|=1 ;

my $tagID = 0 ;

DBQueries::removeAllUsers() ;

#addUser( "test1", "f1", "l1", "asdf\@yahoo.com", "test1" ) ;
#addUser( "test2", "f2", "l2", "asdf\@yahoo.com", "test2" ) ;
#addUser( "test3", "f3", "l3", "asdf\@yahoo.com", "test3" ) ;

DBQueries::removeUser( "testUser" ) ;

$tagID++ ;
if( DBQueries::addUser( "testUser", "firstname", "lastname", "test\@yahoo.com", "password" ) )
{
	print "success in add new user: tag$tagID.... \t\t\t\t SUCCESS\n" ;
}
else
{
	print "fail in add new user: tag$tagID.... \t\t\t\t FAIL\n" ;
}

$tagID++ ;
if( DBQueries::addUser( "testUser", "firstname", "lastname", "test\@yahoo.com", "password" ) )
{
	print "success in add same user: tag$tagID.... \t\t\t\t FAIL\n" ;
}
else
{
	print "fail in add same user: tag$tagID.... \t\t\t\t SUCCESS\n" ;
}

$tagID++ ;
if( DBQueries::removeUser( "testUser" ) )
{
	print "success in remove user: tag$tagID.... \t\t\t\t SUCCESS\n" ;
}
else
{
	print "fail in remove user: tag$tagID.... \t\t\t\t FAIL\n" ;
}

$tagID++ ;
if( DBQueries::removeUser( "testUser" ) )
{
	print "success in remove same user: tag$tagID.... \t\t\t\t FAIL\n" ;
}
else
{
	print "fail in remove same user: tag$tagID.... \t\t\t\t SUCCESS\n" ;
}

$tagID++ ;
if( DBQueries::addUser( "testUser", "firstname", "lastname", "email\@yahoo.com", "password" ) )
{
	print "success in add new user again: tag$tagID.... \t\t\t SUCCESS\n" ;
}
else
{
	print "fail in add new user again: tag$tagID.... \t\t\t FAIL\n" ;
}

$tagID++ ;
if( DBQueries::loginUser( "testUser", "password" ) )
{
	print "success in login user with correct password: tag$tagID.... \t\t SUCCESS\n" ;
}
else
{
	print "fail in login user with correct password: tag$tagID.... \t\t FAIL\n" ;
}

$tagID++ ;
if( DBQueries::loginUser( "testUser", "wrong" ) )
{
	print "success in login user with wrong password: tag$tagID.... \t\t FAIL\n" ;
}
else
{
	print "fail in login user with wrong password: tag$tagID.... \t\t SUCCESS\n" ;
}

$tagID++ ;
if( DBQueries::isUser( "testUser" ) )
{
	print "success in finding user: tag$tagID.... \t\t\t\t SUCCESS\n" ;
}
else
{
	print "fail in finding user: tag$tagID.... \t\t\t\t FAIL\n" ;
}

$tagID++ ;
if( DBQueries::isUser( "fake" ) )
{
	print "success in finding fake user: tag$tagID.... \t\t\t\t FAIL\n" ;
}
else
{
	print "fail in finding fake user: tag$tagID.... \t\t\t\t SUCCESS\n" ;
}

DBQueries::removeFile( "testUser", "newfile" ) ;
$tagID++ ;
if( DBQueries::addFile( "testUser", "newfile" ) )
{
	print "success in adding a new file: tag$tagID.... \t\t\t SUCCESS\n" ;
}
else
{
	print "fail in adding a new file: tag$tagID.... \t\t\t FAIL\n" ;
}

$tagID++ ;
if( DBQueries::addFile( "testUser", "newfile" ) )
{
	print "success in adding an old file: tag$tagID.... \t\t\t\t FAIL\n" ;
}
else
{
	print "fail in adding an old file: tag$tagID.... \t\t\t\t SUCCESS\n" ;
}

$tagID++ ;
if( DBQueries::removeFile( DBQueries::getFileID( "testUser", "newfile" ) ) )
{
	print "success in removing file: tag$tagID.... \t\t\t\t SUCCESS\n" ;
}
else
{
	print "fail in removing file: tag$tagID.... \t\t\t\t FAIL\n" ;
}

DBQueries::addFile( "testUser", "newfile1" ) ;
DBQueries::addFile( "testUser", "newfile2" ) ;
DBQueries::addFile( "testUser", "newfile3" ) ;
$tagID++ ;
if( scalar( DBQueries::getUserFileNames( "testUser" ) ) == 3 )
{
	print "success in getting user files: tag$tagID.... \t\t\t SUCCESS\n" ;
}
else
{
	print "fail in getting user files " . scalar( DBQueries::getUserFileNames( "testUser" ) ) . ": tag$tagID.... \t\t\t FAIL\n" ;
}

$tagID++ ;
if( scalar( DBQueries::removeAllUserFiles( "testUser" ) ) )
{
	print "success in removing all files: tag$tagID.... \t\t\t SUCCESS\n" ;
}
else
{
	print "fail in removing all files: tag$tagID.... \t\t\t FAIL\n" ;
}

$tagID++ ;
if( scalar( DBQueries::getUserFileNames( "testUser" ) ) == 0 )
{
	print "fail in getting user files after delete: tag$tagID.... \t\t SUCCESS\n" ;
}
else
{
	print "success in getting user files after delete " . scalar( DBQueries::getUserFileNames( "testUser" ) ) . ": tag$tagID.... \t\t FAIL\n" ;
}

$tagID++ ;
if( scalar( DBQueries::getUserFileNames( "fake" ) ) == 0 )
{
	print "fail in getting fake user files: tag$tagID.... \t\t\t SUCCESS\n" ;
}
else
{
	print "success in getting fake user files " . scalar( DBQueries::getUserFileNames( "testUser" ) ) . ": tag$tagID.... \t\t\t FAIL\n" ;
}

DBQueries::removeUser( "secondUser" ) ;
DBQueries::addUser( "secondUser", "firstname", "lastname", "test\@gmail.com", "password" ) ;
DBQueries::removeGroup( "newGroup" ) ;
$tagID++ ;
if( DBQueries::addGroup( "testUser", "newGroup" ) )
{
	print "success in adding new group: tag$tagID.... \t\t\t\t SUCCESS\n" ;
}
else
{
	print "fail in adding new group: tag$tagID.... \t\t\t\t FAIL\n" ;
}

DBQueries::removeAllUserFiles( "secondUser" ) ;
DBQueries::addFile( "secondUser", "test1" ) ;
DBQueries::addFile( "secondUser", "test2" ) ;
DBQueries::addFile( "secondUser", "test3" ) ;
DBQueries::addFileToGroup( "newGroup", DBQueries::getFileID( "secondUser", "test1" ) ) ;
DBQueries::addFileToGroup( "newGroup", DBQueries::getFileID( "secondUser", "test2" ) ) ;
DBQueries::addFileToGroup( "newGroup", DBQueries::getFileID( "secondUser", "test3" ) ) ;

$tagID++ ;
if( scalar( DBQueries::getGroupFileNames( "newGroup" ) ) == 3 )
{
	print "success getting group file names: tag$tagID.... \t\t\t SUCCESS\n" ;
}
else
{
	print "fail getting group file names: tag$tagID.... \t\t\t FAIL\n" ;
}

DBQueries::updateUserInfo( "secondUser", "new first name", "new last name", "new email" ) ;

my @test = DBQueries::getUserInfo( "secondUser" ) ; 
$tagID++ ;
if( $test[0] eq "new first name" )
{
	print "success updating user info: tag$tagID.... \t\t\t\t SUCCESS\n" ;
}
else
{
	print "fail updating user info: tag$tagID.... \t\t\t\t FAIL\n" ;
}

DBQueries::addUser( "user2", "asdf", "asdf", "asdf", "asdf" ) ;
DBQueries::addUser( "user3", "asdf", "asdf", "asdf", "asdf" ) ;
DBQueries::addUser( "user4", "asdf", "asdf", "asdf", "asdf" ) ;
DBQueries::addUser( "user5", "asdf", "asdf", "asdf", "asdf" ) ;
DBQueries::addUser( "user6", "asdf", "asdf", "asdf", "asdf" ) ;
DBQueries::addUserToGroup( "user2", "newGroup" ) ;
DBQueries::addUserToGroup( "user3", "newGroup" ) ;
DBQueries::addUserToGroup( "user4", "newGroup" ) ;
DBQueries::addUserToGroup( "user5", "newGroup" ) ;
DBQueries::addUserToGroup( "user6", "newGroup" ) ;

$tagID++ ;
if( scalar( DBQueries::getAllUsers() ) eq 7 )
{
	print "success in returning all users: tag$tagID.... \t\t\t SUCCESS\n" ;
}
else
{
	print "fail in returning all users: tag$tagID.... \t\t\t\t FAIL\n" ;
}

$tagID++ ;
if( scalar( DBQueries::getAllUsers( "newGroup" ) ) eq 1 )
{
	print "success in returning all but group user: tag$tagID.... \t\t SUCCESS\n" ;
}
else
{
	print "fail in returning all but group user: tag$tagID.... \t\t FAIL\n" ;
}

$tagID++ ;
if( scalar( DBQueries::getAllUsers() ) eq 7 )
{
	print "success in returning all users: tag$tagID.... \t\t\t SUCCESS\n" ;
}
else
{
	print "fail in returning all users: tag$tagID.... \t\t\t\t FAIL\n" ;
}

DBQueries::removeInfoID( DBQueries::getFileID( "secondUser", "test1" ) ) ;
DBQueries::addInfo( DBQueries::getFileID( "secondUser", "test1" ), "keyword", "word1", "50" ) ;
DBQueries::addInfo( DBQueries::getFileID( "secondUser", "test1" ), "keyword", "word2", "30" ) ;
DBQueries::addInfo( DBQueries::getFileID( "secondUser", "test1" ), "keyword", "word3", "99" ) ;
DBQueries::addInfo( DBQueries::getFileID( "secondUser", "test1" ), "keyword", "word4", "22" ) ;
DBQueries::addInfo( DBQueries::getFileID( "secondUser", "test1" ), "keyword", "word5", "3" ) ;
DBQueries::addInfo( DBQueries::getFileID( "secondUser", "test1" ), "keyword", "word6", "15" ) ;
DBQueries::addInfo( DBQueries::getFileID( "secondUser", "test1" ), "wordcount", "500", "500" ) ;

$tagID++ ;
if( scalar( DBQueries::getInfoType( DBQueries::getFileID( "secondUser", "test1" ), "keyword" ) ) eq 6 )
{
	print "success in returning key info: tag$tagID.... \t\t\t SUCCESS\n" ;
}
else
{
	print "fail in returning key info: tag$tagID.... \t\t\t FAIL\n" ;
}

$tagID++ ;
if( join( "", DBQueries::getInfoType( DBQueries::getFileID( "secondUser", "test1" ), "wordcount" ) ) eq 500 )
{
	print "success in returning wordcount info: tag$tagID.... \t\t\t SUCCESS\n" ;
}
else
{
	print "fail in returning wordcount info: tag$tagID.... \t\t\t FAIL\n" ;
}

print( join( ", ", DBQueries::getGroupMembers( "newGroup" ) ) ."\n" ) ;
DBQueries::removeGroupMember( "user6", "newGroup" ) ;
print( join( ", ", DBQueries::getGroupMembers( "newGroup" ) ) ."\n" ) ;

DBQueries::addFile( "secondUser", "non-groupfile1" ) ;
DBQueries::addFile( "secondUser", "non-groupfile2" ) ;
print( join( ", ", DBQueries::getGroupFileNames( "newGroup" ) ) . "\n" ) ;
print( join( ", ", DBQueries::getUserFileNames( "secondUser" ) ) . "\n" ) ;
print( join( ", ", DBQueries::getUserFileNames( "secondUser", "newGroup" ) ) . "\n" ) ;

DBQueries::removeInfoID( DBQueries::getFileID( "secondUser", "test1" ) ) ;
DBQueries::removeInfoID( DBQueries::getFileID( "secondUser", "test2" ) ) ;
DBQueries::removeInfoID( DBQueries::getFileID( "secondUser", "test3" ) ) ;
DBQueries::removeInfoID( DBQueries::getFileID( "secondUser", "test4" ) ) ;
DBQueries::removeInfoID( DBQueries::getFileID( "secondUser", "test5" ) ) ;
DBQueries::removeInfoID( DBQueries::getFileID( "secondUser", "test6" ) ) ;
DBQueries::addFile( "testUser", "test4" ) ;
DBQueries::addFile( "testUser", "test5" ) ;
DBQueries::addFile( "testUser", "test6" ) ;
DBQueries::addInfo( DBQueries::getFileID( "secondUser", "test1" ), "keyword", "Japan", "50" ) ;
DBQueries::addInfo( DBQueries::getFileID( "secondUser", "test2" ), "keyword", "Japan", "30" ) ;
DBQueries::addInfo( DBQueries::getFileID( "secondUser", "test3" ), "keyword", "Japan", "99" ) ;
DBQueries::addInfo( DBQueries::getFileID( "testUser", "test4" ), "keyword", "Japan", "22" ) ;
DBQueries::addInfo( DBQueries::getFileID( "testUser", "test5" ), "keyword", "Japan", "3" ) ;
DBQueries::addInfo( DBQueries::getFileID( "testUser", "test6" ), "keyword", "Japan", "15" ) ;

DBQueries::addInfo( DBQueries::getFileID( "secondUser", "test1" ), "wordCount", 90 ) ;
DBQueries::addInfo( DBQueries::getFileID( "secondUser", "test1" ), "avgWordLength", 5 ) ;
DBQueries::addInfo( DBQueries::getFileID( "secondUser", "test1" ), "fileSize", 59 ) ;

print( join( ", ", DBQueries::getInfoType( DBQueries::getFileID( "secondUser", "test1" ), "keyword" ) ) . "\n" ) ;
print( join( ", ", DBQueries::getInfoTypeValueConstraint( "secondUser", "keyword", "Japan", ">", "5" ) ) . "\n" ) ;

my $file_id = DBQueries::getFileID( "secondUser", "test1" ) ;
print( "wordCount: " . join( ", ", DBQueries::getInfoTypeValue( $file_id, "wordCount" ) ) . "\n" ) ;
print( "avgWordLength: " . join( ", ", DBQueries::getInfoTypeValue( $file_id, "avgWordLength" ) ) . "\n" ) ;
print( "fileSize: " . join( ", ", DBQueries::getInfoTypeValue( $file_id, "fileSize" ) ) . "\n" ) ;

print( join( ", ", DBQueries::addUserToGroup( "secondUser", "newGroup" ) ) . "\n" ) ;
print( join( ", ", DBQueries::getUserGroups( "secondUser" ) ) . "\n" ) ;
print( join( ", ", DBQueries::getAllUserFileNames( "secondUser") ) . "\n" ) ;

